﻿using Prism.Navigation;
using Prism.Regions;
using System.Threading;

namespace ZYing.QPanel.ViewModels
{
    public abstract class NavigationViewModelBase : ViewModelBase, INavigationAware, IDestructible
    {
        #region IDestructible
        public virtual void Destroy()
        {
            if (this.CancellationTokenSource.IsCancellationRequested) return;
            this.CancellationTokenSource.Cancel();
        }
        #endregion

        #region INavigationAware
        public virtual bool IsNavigationTarget(NavigationContext navigationContext)
        {
            return true;
        }

        public virtual void OnNavigatedFrom(NavigationContext navigationContext)
        {
            this.IsActive = false;
        }

        public virtual void OnNavigatedTo(NavigationContext navigationContext)
        {
            this.IsActive = true;
        }
        #endregion

        protected CancellationTokenSource CancellationTokenSource { get; } = new CancellationTokenSource();
        private bool _isActive = true;

        public bool IsActive
        {
            get { return _isActive; }
            private set { SetProperty(ref _isActive, value); }
        }
    }
}
